import Vue from "vue";
import VueRouter from "vue-router";
import { userJwt } from "@/utils/request";
import { component } from "vue/types/umd";

Vue.use(VueRouter);

// 解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题。
// const originalPush = VueRouter.prototype.push;
// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch((err) => err);
// };

const routes = [
  {
    path: "/Manager",
    name: "Manager",
    component: () => import("../views/Manager.vue"),
    redirect: "/Manager/home", // 重定向到主页
    children: [
      {
        path: "403",
        name: "NoAuth",
        meta: { name: "无权限" },
        component: () => import("../views/manager/403.vue"),
      },
      {
        path: "home",
        name: "Home",
        meta: { name: "系统首页" },
        component: () => import("../views/manager/Home.vue"),
      },
      {
        path: "admin",
        name: "Admin",
        meta: { name: "管理员信息" },
        component: () => import("../views/manager/Admin.vue"),
      },
      {
        path: "adminPerson",
        name: "AdminPerson",
        meta: { name: "个人信息" },
        component: () => import("../views/manager/AdminPerson.vue"),
      },
      {
        path: "password",
        name: "Password",
        meta: { name: "修改密码" },
        component: () => import("../views/manager/Password.vue"),
      },
      {
        path: "notice",
        name: "Notice",
        meta: { name: "公告信息" },
        component: () => import("../views/manager/Notice.vue"),
      },
      {
        path: "user",
        name: "User",
        meta: { name: "用户信息" },
        component: () => import("../views/manager/User.vue"),
      },
      {
        path: "category",
        name: "Category",
        meta: { name: "资讯分类" },
        component: () => import("../views/manager/Category.vue"),
      },
      {
        path: "news",
        name: "News",
        meta: { name: "资讯信息" },
        component: () => import("../views/manager/News.vue"),
      },
      {
        path: "activity",
        name: "Activity",
        meta: { name: "养老活动" },
        component: () => import("../views/manager/Activity.vue"),
      },
      {
        path: "serve",
        name: "Serve",
        meta: { name: "养老服务" },
        component: () => import("../views/manager/Serve.vue"),
      },
      {
        path: "nurseHouse",
        name: "NurseHouse",
        meta: { name: "养老院信息" },
        component: () => import("../views/manager/NurseHouse.vue"),
      },
      /* {
        path: "comment",
        name: "Comment",
        meta: { name: "评论信息" },
        component: () => import("../views/manager/Comment"),
      }, */
      {
        path: "activitySign",
        name: "ActivitySign",
        meta: { name: "活动报名" },
        component: () => import("../views/manager/ActivitySign.vue"),
      },
      {
        path: "reserve",
        name: "Reserve",
        meta: { name: "服务预约" },
        component: () => import("../views/manager/Reserve.vue"),
      },
      {
        path: "order-beojie",
        name: "order-beojie",
        meta: { name: "保洁订单" },
        component: () => import("@/views/manager/order/order-baojie"),
      },
      {
        path: "order-canyin",
        name: "order-canyin",
        meta: { name: "餐饮订单" },
        component: () => import("@/views/manager/order/order-canyin"),
      },
      {
        path: "order-peihu",
        name: "order-peihu",
        meta: { name: "陪护订单" },
        component: () => import("@/views/manager/order/order-peihu.vue"),
      },
    ],
  },
  {
    path: "/front",
    name: "Front",
    redirect: "/front/home",
    component: () => import("../views/Front.vue"),
    children: [
      {
        path: "home",
        name: "home",
        meta: { name: "系统首页" },
        component: () => import("../views/front/Home.vue"),
      },
      {
        path: "person",
        name: "FPerson",
        meta: { name: "个人信息" },
        component: () => import("../views/front/Person.vue"),
      },
      {
        path: "newsDetail",
        name: "FNewsDetail",
        component: () => import("../views/front/NewsDetail.vue"),
      },
      {
        path: "activityDetail",
        name: "FActivityDetail",
        component: () => import("../views/front/ActivityDetail.vue"),
      },
      {
        path: "activity",
        name: "FActivity",
        component: () => import("../views/front/Activity.vue"),
      },
      {
        path: "activitySign",
        name: "FActivitySign",
        component: () => import("../views/front/ActivitySign.vue"),
      },

      {
        path: "reserve",
        name: "FReserve",
        component: () => import("../views/front/Reserve.vue"),
      },
      {
        path: "nurseHouse",
        name: "FNurseHouse",
        component: () => import("../views/front/NurseHouse.vue"),
      },
      {
        path: "houseDetail",
        name: "HouseDetail",
        component: () => import("../views/front/HouseDetail"),
      },

      {
        path: "serve",
        name: "FServe",
        component: () => import("@/views/front/Serve/index.vue"),
      },
      {
        path: "select_service",
        name: "select_service",
        component: () => import("@/views/front/Serve/select_service"),
        children: [
          // 日常保洁
          {
            path: "rcbj-serve-detail",
            name: "rcbj-serve-detail",
            component: () =>
              import("@/views/front/Serve/rcbj-serve-detail.vue"),
          },
          {
            path: "rcbj-form",
            name: "rcbj-form",
            component: () => import("@/views/front/Serve/rcbj-form.vue"),
          },
          // 厨房保养
          {
            path: "chufang-serve-dateil",
            name: "chufang-serve-dateil",
            component: () =>
              import("@/views/front/Serve/chufang/chufang-serve-dateil.vue"),
          },
          {
            path: "chufang-form",
            name: "chufang-form",
            component: () =>
              import("@/views/front/Serve/chufang/chufang-form.vue"),
          },
          // 卫生间
          {
            path: "weishengjian-serve-detail",
            name: "weishengjian-serve-detail",
            component: () =>
              import(
                "@/views/front/Serve/weishengjian/weishengjian-serve-detail.vue"
              ),
          },
          {
            path: "weishengjian-form",
            name: "weishengjian-form",
            component: () =>
              import("@/views/front/Serve/weishengjian/weishengjian-form.vue"),
          },
          // 擦玻璃
          {
            path: "chaboli-serve-detail",
            name: "chaboli-serve-detail",
            component: () =>
              import("@/views/front/Serve/chaboli/chaboli-serve-detail.vue"),
          },
          {
            path: "chaboli-form",
            name: "chaboli-form",
            component: () =>
              import("@/views/front/Serve/chaboli/chaboli-form.vue"),
          },
          // 下单成功
          {
            path: "orderResult",
            name: "orderResult",
            component: () => import("@/views/front/Serve/orderResult.vue"),
          },
        ],
      },
      {
        path: "serve-order",
        name: "serve-order",
        component: () => import("@/views/front/Serve/order/index.vue"),
        children: [
          {
            path: "serve-order-zao",
            name: "serve-order-zao",
            component: () => import("@/views/front/Serve/order/zao/zao.vue"),
          },
          {
            path: "serve-order-from",
            name: "serve-order-from",
            component: () =>
              import("@/views/front/Serve/order/zao/order-from.vue"),
          },
          // 下单成功
          {
            path: "orderResult",
            name: "orderResult",
            component: () => import("@/views/front/Serve/orderResult.vue"),
          },
        ],
      },
      {
        path: "serve-peihu",
        name: "serve-peihu",
        component: () => import("@/views/front/Serve/peihu/index.vue"),
        children: [
          {
            path: "peihu-yuding",
            name: "peihu-yuding",
            component: () =>
              import("@/views/front/Serve/peihu/peihu-yuding.vue"),
          },
        ],
      },
      {
        path: "user-order-all",
        name: "user-order-all",
        component: () => import("@/views/front/user-order-all/index.vue"),
      },
    ],
  },
  {
    path: "/login",
    name: "Login",
    meta: { name: "登录" },
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/register",
    name: "Register",
    meta: { name: "注册" },
    component: () => import("../views/Register.vue"),
  },
  {
    path: "*",
    name: "NotFound",
    meta: { name: "无法访问" },
    component: () => import("../views/404.vue"),
  },
];

const rootRoute = {
  path: "/",
  name: "Root",
  redirect: "/front",
};

const pay = [
  {
    path: "/pay-success",
    name: "pay-success",
    component: () => import("@/views/pay-success.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: "/",
  routes: [rootRoute, ...routes, ...pay],
});

const jwtRoutes = [
  "/front/select_service",
  "/front/activityDetail",
  "/front/serve-order",
  "orderResult",
  "Manager",
  "front/person",
];

// 注：不需要前台的项目，可以注释掉该路由守卫
// 路由守卫
// router.beforeEach(async (to, from, next) => {
//   let user = JSON.parse(localStorage.getItem("xm-user") || "{}");
//   if (to.path === "/") {
//     if (user.role) {
//       if (user.role === "USER") {
//         next("/front/home");
//       } else {
//         next("/home");
//       }
//     } else {
//       next("/login");
//     }
//   } else {
//     next();
//   }
// });
router.beforeEach(async (to, from, next) => {
  if (to.path === "/" || to.path === "/login" || to.path === "/register") {
    return next();
  }
  let is = true;
  jwtRoutes.forEach((v) => {
    if (v.startsWith("/")) {
      if (v === to.path) {
        is = false;
      }
    } else {
      if (to.path.includes(v)) {
        is = false;
      }
    }
  });

  if (!is) {
    const res = await userJwt();
    if (!res) {
      return next({
        path: "/login",
        query: { redirect: to.fullPath }, // 传递重定向地址
      });
    }
  }

  next();
});
export default router;
